/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 10-feb-2011, 20.49.54
 */

package biblio.scanner;

import biblio.BiblioView;
import biblio.accounts.google.Util;
import java.awt.HeadlessException;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import org.loftjob.engine.Engine;
import org.loftjob.model.Book;

/**
 *
 * @author mila
 */
public class ScannerView extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public ScannerView() {
        {
             initComponents();
            FileInputStream reader = null;
            try {
                Properties prop = new Properties();
                reader = new FileInputStream(new File("BiblioOp.properties"));
                prop.load(reader);
                path = (String) prop.get("data");
                String tmpImage = (String) prop.getProperty("tmpImage");
                URL imagePath = new File(path + File.separator + tmpImage).toURI().toURL();
                icon = new ImageIcon(imagePath);
                Image imageTmp = icon.getImage().getScaledInstance(jLabel2.getWidth()-30, jLabel2.getHeight()-30, Image.SCALE_DEFAULT);
                icon = new ImageIcon(imageTmp);
               
                book = new Book();
                book.setCover(path + File.separator + tmpImage);
                this.setBook(book);
            } catch (IOException ex) {
                Logger.getLogger(ScannerView.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(ScannerView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.setResizable(false);
            //this.pack();
        }
    }

    public ScannerView(String string) throws HeadlessException {
        super(string);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(600, 400));
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(biblio.BiblioApp.class).getContext().getResourceMap(ScannerView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jLabel1.border.title"))); // NOI18N
        jLabel1.setMaximumSize(new java.awt.Dimension(300, 400));
        jLabel1.setMinimumSize(new java.awt.Dimension(300, 400));
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setPreferredSize(new java.awt.Dimension(300, 400));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jLabel2.border.title"))); // NOI18N
        jLabel2.setMaximumSize(new java.awt.Dimension(300, 400));
        jLabel2.setMinimumSize(new java.awt.Dimension(300, 400));
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setPreferredSize(new java.awt.Dimension(300, 400));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), resourceMap.getString("jTextArea1.border.title"))); // NOI18N
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText(resourceMap.getString("Exit.text")); // NOI18N
        jButton1.setName("Exit"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit(evt);
            }
        });

        jButton2.setText(resourceMap.getString("Cancel.text")); // NOI18N
        jButton2.setName("Cancel"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel(evt);
            }
        });

        jButton3.setText(resourceMap.getString("Save.text")); // NOI18N
        jButton3.setName("Save"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(149, 149, 149)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 784, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(512, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   public void setScanner(ImageIcon icon) {
       int width = new Double(this.jLabel1.getPreferredSize().getWidth()).intValue();
       int height = new Double(this.jLabel1.getPreferredSize().getHeight()).intValue();
      // Image img = icon.getImage().getScaledInstance(width, height-90,Image.SCALE_DEFAULT);
        this.jLabel1.setIcon(new ImageIcon(icon.getImage().getScaledInstance(width-30, height-30,Image.SCALE_DEFAULT)));
    }

   public void setBook(Book book) {
        try {
            this.book = book;
            this.jTextArea1.selectAll();
            this.jTextArea1.cut();
            this.jTextArea1.setText(this.book.toString());
            Image imageCover = ImageIO.read(new URL("file://" + new File(this.book.getCover().trim()).getAbsolutePath()));
            if (imageCover != null) {
                ImageIcon imageIcon = new ImageIcon(imageCover.getScaledInstance(jLabel2.getWidth()-30, jLabel2.getHeight()-30, Image.SCALE_DEFAULT));
                this.jLabel2.setIcon(imageIcon);
            } else {
                this.jLabel2.setIcon(this.icon);
            }
        } catch (IOException ex) {
            Logger.getLogger(ScannerView.class.getName()).log(Level.SEVERE, null, ex);
        }
   }





    private void save(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save
        if (book.getCover() != null) {
                            try {
                                Util.saveImage(book, new URL("file://" + book.getCover()));
                            } catch (MalformedURLException ex) {
                                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
    }//GEN-LAST:event_save

    private void exit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit
       this.dispose();
    }//GEN-LAST:event_exit

    private void cancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel
       this.jLabel2.setIcon(icon);
        this.jTextArea1.selectAll();
        this.jTextArea1.cut();
        this.jTextArea1.setText(new Book().toString());
    }//GEN-LAST:event_cancel

   
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
    private Book book = new Book();
    private String path;
    private ImageIcon icon;
}
